package com.ruowei.modules.sys.web.task;

import com.ruowei.modules.member.domain.Question;
import com.ruowei.modules.member.repository.QuestionRepository;
import com.ruowei.modules.sys.domain.SysConfig;
import com.ruowei.modules.sys.domain.SysUserRole;
import com.ruowei.modules.sys.domain.enumeration.RoleStatusType;
import com.ruowei.modules.sys.domain.enumeration.RoleType;
import com.ruowei.modules.sys.domain.enumeration.UserStatusType;
import com.ruowei.modules.sys.domain.enumeration.UserType;
import com.ruowei.modules.sys.domain.table.SysRole;
import com.ruowei.modules.sys.domain.table.SysUserTable;
import com.ruowei.modules.sys.repository.SysConfigRepository;
import com.ruowei.modules.sys.repository.SysRoleRepository;
import com.ruowei.modules.sys.repository.SysUserRepository;
import com.ruowei.modules.sys.repository.SysUserRoleRepository;
import com.ruowei.modules.sys.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Optional;

@Component
@Slf4j
public class InitDataTask {
    private final SysUserRepository sysUserRepository;
    private final PasswordEncoder passwordEncoder;
    private final SysRoleRepository sysRoleRepository;
    private final SysUserRoleRepository sysUserRoleRepository;
    private final SysConfigRepository sysConfigRepository;
    private final QuestionRepository questionRepository;

    public InitDataTask(SysUserRepository sysUserRepository, PasswordEncoder passwordEncoder, SysRoleRepository sysRoleRepository, SysUserRoleRepository sysUserRoleRepository, SysConfigRepository sysConfigRepository, QuestionRepository questionRepository) {
        this.sysUserRepository = sysUserRepository;
        this.passwordEncoder = passwordEncoder;
        this.sysRoleRepository = sysRoleRepository;
        this.sysUserRoleRepository = sysUserRoleRepository;
        this.sysConfigRepository = sysConfigRepository;
        this.questionRepository = questionRepository;
    }


    @Scheduled(initialDelay = 500, fixedDelay = Long.MAX_VALUE)
    public void initData() {
        log.info("执行数据库初始化");

        //初始化系统管理员
        if (!sysUserRepository.existsByUserCode("8c113635-b505-461e-a464-3a5e5d774144")) {
            SysUserTable sysUserTable = new SysUserTable();
            sysUserTable.setLoginCode("admin");
            sysUserTable.setUserName("系统管理员");
            sysUserTable.setUserCode("8c113635-b505-461e-a464-3a5e5d774144");
            sysUserTable.setPassword(passwordEncoder.encode("123456"));
            sysUserTable.setUserType(UserType.ADMIN);
            sysUserTable.setStatus(UserStatusType.NORMAL);
            sysUserRepository.save(sysUserTable);
            Optional<SysRole> roleOptional = sysRoleRepository.getOneByRoleCode("ROLE_ADMIN_XTGL");
            SysRole sysRole;
            if (!roleOptional.isPresent()) {
                SysRole role = new SysRole();
                role.setIsSys(true);
                role.setRemarks("系统管理员");
                role.setRoleCode("ROLE_ADMIN_XTGL");
                role.setRoleName("系统管理员");
                role.setRoleType(RoleType.USER);
                role.setStatus(RoleStatusType.NORMAL);
                sysRole = sysRoleRepository.save(role);
            } else {
                sysRole = roleOptional.get();
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysRoleId(sysRole.getId());
            sysUserRole.setSysUserId(sysUserTable.getId());
            sysUserRoleRepository.save(sysUserRole);
            log.info("系统管理员创建成功");
        }

        //初始化调度站
        if (!sysUserRepository.existsByUserCode("b6dc09a232324b878412d899a58603a5")) {
            SysUserTable sysUserTable = new SysUserTable();
            sysUserTable.setLoginCode("枫彩调度站");
            sysUserTable.setUserName("枫彩调度站");
            sysUserTable.setUserCode("b6dc09a232324b878412d899a58603a5");
            sysUserTable.setPassword(passwordEncoder.encode("123456"));
            sysUserTable.setUserType(UserType.STATION);
            sysUserTable.setStatus(UserStatusType.NORMAL);
            sysUserRepository.save(sysUserTable);
            Optional<SysRole> roleOptional = sysRoleRepository.getOneByRoleCode("ROLE_DISPATCHING_STATION");
            SysRole sysRole;
            if (!roleOptional.isPresent()) {
                SysRole role = new SysRole();
                role.setIsSys(true);
                role.setRemarks("调度站角色");
                role.setRoleCode("ROLE_DISPATCHING_STATION");
                role.setRoleName("调度站");
                role.setRoleType(RoleType.USER);
                role.setStatus(RoleStatusType.NORMAL);
                sysRole = sysRoleRepository.save(role);
            } else {
                sysRole = roleOptional.get();
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysRoleId(sysRole.getId());
            sysUserRole.setSysUserId(sysUserTable.getId());
            sysUserRoleRepository.save(sysUserRole);
            log.info("调度站创建成功");
        }

        //初始化SysConfig
        if (!sysConfigRepository.existsByConfigKey("ORDERINTERVAL")) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigValue("10");
            sysConfig.setConfigKey("ORDERINTERVAL");
            sysConfig.setConfigName("约车时间间隔");
            sysConfig.setRemark("分钟");
            sysConfig.setSetDate(Instant.now());
            sysConfigRepository.save(sysConfig);
        }
        if (!sysConfigRepository.existsByConfigKey("TEXTTIPS")) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigValue("下单成功，工作人员将与您联系，如有疑问请拨打电话：");
            sysConfig.setConfigKey("TEXTTIPS");
            sysConfig.setConfigName("文本提示");
            sysConfig.setRemark("");
            sysConfig.setSetDate(Instant.now());
            sysConfigRepository.save(sysConfig);
        }
        if (!sysConfigRepository.existsByConfigKey("PHONE")) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigValue("15000000001");
            sysConfig.setConfigKey("PHONE");
            sysConfig.setConfigName("联系电话");
            sysConfig.setRemark("调度站联系电话");
            sysConfig.setSetDate(Instant.now());
            sysConfigRepository.save(sysConfig);
        }

        //初始化帮助反馈数据
        if (!questionRepository.existsByQuestionType("点餐")){

            saveQuestion("如何取消订单",
                "用户您好，非常抱歉给您带来不便。如需取消订单请按以下步骤操作：\\n\\n1.未支付->进入订单列表->待支付->找到您需要取消的订单->点击订单进入详情->点击取消订单按钮，取消成功您会看见对应提示。\\n\\n2.已支付、商家还未接单->订单列表->进行中->点击订单进入详情->取消订单，相应款项会原路返回您的账户。\\n\\n3.已支付、商家或者配送员已接单->同样操作进入订单详情页点击申请退款即可。\\n\\n感谢您的谅解~",
                "点餐");

            saveQuestion("配送时间过长",
                "用户您好，非常抱歉给您带来不便。订单下单成功后配送员或商家会尽快为您配送，如配送时间过长您可以按照以下方式处理：\\n\\n1.请在订单列表全部订单中寻找到您的订单，点击订单进入详情,若您的订单没有配送员信息，您可以通过商家电话咨询订单配送进度。\\n\\n2.若您的订单已有配送员信息，您可以根据配送员电话联系配送员进行催促。\\n\\n感谢您的谅解~",
                "点餐");

            saveQuestion("如何联系商家",
                "用户您好，请在订单列表寻找到您的订单，点击订单进入详情可以查看商家电话或者配送员电话。",
                "点餐");

            saveQuestion("错送漏送商品",
                "用户您好，非常抱歉给您带来不便。请在订单列表寻找到您的订单，点击订单进入详情查看商家电话，及时与商家联系\\n\\n感谢您的谅解~",
                "点餐");

            saveQuestion("商家拒绝退单",
                "用户您好，非常抱歉给您带来不便。可能是配送员已经在给您送餐的路上。若您仍要退单，请联系园区内工作人员。\\n\\n感谢您的谅解~",
                "点餐");

            saveQuestion("如何联系配送员",
                "用户您好，请在订单列表寻找到您的订单，点击订单进入详情可以查看配送员电话。",
                "点餐");
        }

        if (!questionRepository.existsByQuestionType("约车")) {
            saveQuestion("如何获取当前地址",
                "用户您好，若您未使用扫码进入，请在预约车界面点击选择地址，在弹出框内使用扫一扫，扫描离您最近的二维码来获取您当前所处的位置。",
                "约车");
        }
    }

    private void saveQuestion(String questionDes, String answer, String type) {
        Question question = new Question();
        question.setQuestionCode(CommonUtils.getUUID());
        question.setQuestion(questionDes);
        question.setAnswer(answer);
        question.setQuestionType(type);
        questionRepository.save(question);
    }
}
